#include "filewidget.h"
#include "ui_filewidget.h"
#include <QHeaderView>
#include <QComboBox>
#include <QHBoxLayout>
#include <QVariant>
#include <QStackedWidget>
#include <QDesktopServices>
#include "../CheckInfor/DataBase/keywordlib.h"
#include "../CheckInfor/fileinfor.h"
#include "./ItemDelegate/mydetegate.h"
#include "../CheckTool/filefilter.h"
#include <QStringList>
#include <iostream>
#include <QFileDialog>
#include"../Widget/filejudge.h"
#include<QToolTip>
#define MODELPATHS "../conf/default/fileaimg.conf"


const QStringList FileWidget::FilePwLevel{
    "绝密","机密","秘密","内部","公开","待鉴定"
};

const QStringList FileWidget::IllegaLevel{
//    "正常","怀疑","不合法"
   "违规","合规","未判定"
};
const QStringList labels={"扫描文件数:","检查文件数:","命中文件数:"};

FileWidget::FileWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FileWidget)
{
    ui->setupUi(this);
    Init_Ui();
//    ui->stackedWidget->setCurrentIndex(1);
}

FileWidget::~FileWidget()
{
    delete ui;
}

void FileWidget::Init_Ui(){

    QTextStream& fstream=Yzty::GetTextSteam(MODELPATHS);
    QStringList items=fstream.readAll().split('#');
    items.removeFirst();
    auto&& pstruct=Yzty::ConfigParse_Struct(items);
    Init_Config(pstruct);


//右键菜单
    this->_tableviewmenu=new QMenu(this);

    this->_plevelmenu=new QMenu("密级判定",this->_tableviewmenu);

    for(int i=0;i<FileWidget::FilePwLevel.size();i++){
        _plevel[i]=new QAction(FilePwLevel[i]);
        _plevel[i]->setData(i); //设置标记
        connect(_plevel[i],SIGNAL(triggered()),this,SLOT(FileWidget::LevelJudge(i)));
        _plevelmenu->addAction(_plevel[i]);
    }
    this->_tableviewmenu->addMenu(this->_plevelmenu);
    connect(_plevelmenu,&QMenu::triggered,this,&FileWidget::Judge);
    this->_openfilelocation=new QAction("打开文件夹",this->_tableviewmenu);
    this->_tableviewmenu->addAction(this->_openfilelocation);
 //   connect(_openfilelocation,SIGNAL(QAction::triggered(QAction*)),this,SLOT(FileWidget::OpenFileLocation(QAction*)));
    connect(_openfilelocation,&QAction::triggered,this,&FileWidget::OpenFileLocation);
    this->_openfile=new QAction("打开文件",this->_tableviewmenu);
    this->_tableviewmenu->addAction(this->_openfile);
    connect(_openfile,&QAction::triggered,this,&FileWidget::OpenFile);

    const QMap<Yzty::WordLibCode,QString>& types=KeyWordLib::GetInstance()->GetLibNameList();
    for(int i=0;i<_views.size();i++){
        for(int j=0;j<_views[i].size();j++){

            _plains[i][j]->setReadOnly(true);
          _comboxs[i][j]->addItem("所有类型",-1);
            _comboxs[i][j]->setCurrentIndex(0);
            for(Yzty::WordLibCode code:types.keys()){
                _comboxs[i][j]->addItem(types[code],QVariant(int(code)));
                connect(_comboxs[i][j],QOverload<int>::of(&QComboBox::currentIndexChanged),[this,i,j](int){
                    this->KeyWord_Combox_Change(i,j);   //根据关键词类型显示不同的命中率
                });
            }

            _views[i][j]->setContextMenuPolicy(Qt::CustomContextMenu);//设置右键菜单策略
            connect(_views[i][j],&QTableView::customContextMenuRequested,[this,i](const QPoint& pos){
                if(i!=ui->stackedWidget->currentIndex())
                    return;
                this->MouseRightMenu(pos);
            });

            _views[i][j]->setSelectionBehavior(QAbstractItemView::SelectRows);
            _views[i][j]->setSelectionMode(QAbstractItemView::SingleSelection);
            _views[i][j]->setEditTriggers(QAbstractItemView::NoEditTriggers);
            connect(_views[i][j],&QTableView::clicked,[this,i,j](const QModelIndex& index){
                this->RowSelect_Paint(index.row(),i,j);
            });
  //          connect(_views[i][j],&QTableView::doubleClicked,this,&FileWidget::OpenFile);

            connect(_models[i][j],&QStandardItemModel::dataChanged,[this,i,j](const QModelIndex& index1,const QModelIndex& index2,const QVector<int>& vec){
                int row=index2.row();
                QModelIndex index=_models[i][j]->index(row,0);
                int mcode=index.data(Qt::UserRole+1).toInt()/Yzty::ToolMapItemHashCode;
                QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode(mcode)));
                Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(infors[row]);
                if(index1.column()==8){
                    int sp=0;
                    infor->_result=index1.data().toString();
                    std::cout<<infor->_result.toStdString()<<std::endl;
                    if(infor->_result==FileWidget::IllegaLevel[0]) sp=int(Yzty::FileToolCode::FOTHER);
                    else if(infor->_result==FileWidget::IllegaLevel[1]) sp=int(Yzty::FileToolCode::FDOUBT);
                    else if(infor->_result==FileWidget::IllegaLevel[2]) sp=int(Yzty::FileToolCode::FILLEGAL);
                    FileInfor::GetInstance()->SwapClassfity(mcode,row,sp);
                }
                UpdateData(i,j);
            });

        }

        //tab页切换时
        connect(_tabws[i],&QTabWidget::currentChanged,[this,i](int index){
            this->UpdateData(i,index);
        });
    }
}

void FileWidget::SetCurWidget(int i){
    ui->stackedWidget->setCurrentIndex(i);
    UpdateData(i);
}


void FileWidget::Init_Config(QMap<int,QVector<QPair<QString,QStringList>>>& pstruct){

    int n=pstruct.size();

    _models=QVector<QVector<QStandardItemModel*>>(n);
    _views=QVector<QVector<QTableView*>>(n);
    _plains=QVector<QVector<QPlainTextEdit*>>(n);
    _comboxs=QVector<QVector<QComboBox*>>(n);
    _model_labels=QVector<QVector<QStringList>>(n);
    QVector<QLabel*> tempnumbers(labels.size());


    int m=FileWidget::FilePwLevel.size();
    _plevel=QVector<QAction*>(m);

    for(int i=0;i<n;i++){
        QWidget* w=new QWidget(this);
        ui->stackedWidget->addWidget(w);
        QGridLayout* layout=new QGridLayout(w);
        QTabWidget* tabw=new QTabWidget(this);
        _tabws.push_back(tabw);

        for(auto& temp:pstruct[i]){
            QWidget* tw=new QWidget(tabw);
            tabw->addTab(tw,temp.first);
            QStandardItemModel* model=new QStandardItemModel(tw);
            QTableView*         view =new QTableView(tw);
            QPlainTextEdit*     edit =new QPlainTextEdit(tw);
            QComboBox*          combox=new QComboBox(tw);
            QLabel*             label=new QLabel(QString("关键词类型："));
            view->setMouseTracking(true);
            connect(view, &QTableView::entered, this, &FileWidget::doMouseTrackTip);
            label->setMaximumHeight(20);
            combox->setMaximumHeight(20);
            view->setMinimumHeight(300);
            edit->setMinimumHeight(100);
            edit->setMaximumHeight(200);

            QVBoxLayout* vlayout=new QVBoxLayout(tw);
            QHBoxLayout* hlayout=new QHBoxLayout();
             for(int k=0;k<labels.size();k++){
                 tempnumbers[k]=new QLabel(tw);
                 tempnumbers[k]->setText(labels[k]);
                 tempnumbers[k]->setVisible(true);
                 tempnumbers[k]->setMaximumHeight(20);
                 tempnumbers[k]->setMinimumWidth(60);
                 hlayout->addWidget(tempnumbers[k],3);
            }
             _numbers.push_back(tempnumbers);
        //    hlayout->addSpacerItem(new QSpacerItem(100,10,QSizePolicy::Fixed));
            hlayout->addStretch(2);
            hlayout->addWidget(label,1);
            hlayout->addWidget(combox,1);
            vlayout->addWidget(view);
            vlayout->addLayout(hlayout);
            vlayout->addWidget(edit);
            view->setModel(model);
            model->clear();
            _models[i].push_back(model);
            _views[i].push_back(view);
            _plains[i].push_back(edit);
            _comboxs[i].push_back(combox);

            view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//            view->setEditTriggers(QAbstractItemView::NoEditTriggers);
            model->setHorizontalHeaderLabels(temp.second);
            _model_labels[i].push_back(temp.second);
            tw->setLayout(layout);
        }

        layout->addWidget(tabw);
        w->setLayout(layout);
    }
}

void FileWidget::UpdateData(Yzty::MODECODE code,Yzty::ITEMCODE tcode){
    _updatemutex.lock();
 //   this->UpDatecnts();
    ClearModel(code,tcode);
    switch (Yzty::FileToolCode(code))
    {
        case Yzty::FileToolCode::FTOTAL:
            UpDateFTotal(code,tcode);
            break;
        case Yzty::FileToolCode::FKEY:
            UpDateFKey(code,tcode);
            break;
        case Yzty::FileToolCode::FDOUBT:
            UpDateFDoubt(code,tcode);
            break;
        case Yzty::FileToolCode::FOTHER:
            UpDateFOther(code,tcode);
            break;
        case Yzty::FileToolCode::FILLEGAL:
            UpDateFIllegal(code,tcode);
            break;
        case Yzty::FileToolCode::PTOTAL:
            UpDatePTotal(code,tcode);
            break;
        case Yzty::FileToolCode::PKEY:
            UpDatePKey(code,tcode);
            break;
        case Yzty::FileToolCode::PDOUBT:
            UpDatePDoubt(code,tcode);
            break;
        case Yzty::FileToolCode::POTHER:
            UpDatePOther(code,tcode);
            break;
        case Yzty::FileToolCode::PILLEGAL:
            UpDatePIllegal(code,tcode);
            break;
        default:
            break;
    }
    _updatemutex.unlock();
}

void FileWidget::ClearAllModel(){
    for(int i=0;i<_models.size();i++)
        for(int j=0;j<_models[i].size();j++)
            ClearModel(i,j);
}

void FileWidget::ClearModel(Yzty::MODECODE code, Yzty::ITEMCODE tcode){
    _models[code][tcode]->clear();
    _models[code][tcode]->setHorizontalHeaderLabels(_model_labels[code][tcode]);
    _plains[code][tcode]->clear();
}

void FileWidget::UpDateFTotal(Yzty::MODECODE code,Yzty::ITEMCODE tcode){

    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::FTOTAL));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型
        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数
        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::FTOTAL)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));
        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDateFKey(Yzty::MODECODE code,Yzty::ITEMCODE tcode){

    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::FKEY));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::FKEY)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));
        _models[code][tcode]->appendRow(items);
    }
}

void FileWidget::UpDateFDoubt(Yzty::MODECODE code,Yzty::ITEMCODE tcode){

    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::FDOUBT));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));

        items[0]->setData(QVariant(int(Yzty::FileToolCode::FDOUBT)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));

        _models[code][tcode]->appendRow(items);
    }
}

void FileWidget::UpDateFOther(Yzty::MODECODE code,Yzty::ITEMCODE tcode){

    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::FOTHER));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));

        items[0]->setData(QVariant(int(Yzty::FileToolCode::FOTHER)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));

        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDateFIllegal(Yzty::MODECODE code,Yzty::ITEMCODE tcode){

    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::FILLEGAL));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items.push_back(new QStandardItem("无"));
        items.push_back(new QStandardItem(infor->_descripe));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::FILLEGAL)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));

        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDatePTotal(Yzty::MODECODE code,Yzty::ITEMCODE tcode){
    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::PTOTAL));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::PTOTAL)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));
        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDatePKey(Yzty::MODECODE code,Yzty::ITEMCODE tcode){
    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::PKEY));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::PKEY)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));
        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDatePDoubt(Yzty::MODECODE code,Yzty::ITEMCODE tcode){
    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::PDOUBT));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::PDOUBT)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));
        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDatePOther(Yzty::MODECODE code,Yzty::ITEMCODE tcode){
    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::POTHER));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::POTHER)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));
        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDatePIllegal(Yzty::MODECODE code,Yzty::ITEMCODE tcode){

    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode::PILLEGAL));
    //设置代理类
    MyDelegate* delegate=new MyDelegate(7,FileWidget::FilePwLevel);
    _views[code][tcode]->setItemDelegateForColumn(7,delegate);

    delegate=new MyDelegate(8,FileWidget::IllegaLevel);
    _views[code][tcode]->setItemDelegateForColumn(8,delegate);

    for(Yzty::BaceInfor* temp:infors){
        Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(temp);
        QList<QStandardItem*> items;
        items.push_back(new QStandardItem(infor->_filetname));//文件名称
        items.push_back(new QStandardItem(infor->_filetype));//文件类型

        int keyid=_comboxs[code][tcode]->currentData().toInt();
        int totalnums=0;

        if(keyid!=-1)
            totalnums=KeyWordLib::GetInstance()->GetKeyWordNums(keyid);
        else
            totalnums=FileInfor::GetInstance()->GetTotalKeyWords();

        items.push_back(new QStandardItem(QString("%1/%2").arg(infor->_hits[keyid]).arg(totalnums))); //命中个数

        items.push_back(new QStandardItem(infor->_filepath));   //文件路径
        items.push_back(new QStandardItem(infor->_filesize));   //文件大小
        items.push_back(new QStandardItem(infor->_createtime));   //创建时间
        items.push_back(new QStandardItem(infor->_lastmodifytime));//修改时间

//        for(QStandardItem* item:items)
//                item->setEnabled(false);

        items.push_back(new QStandardItem(infor->_pwlevel));
        items.push_back(new QStandardItem(infor->_result));
        items.push_back(new QStandardItem(infor->_evaluator));
        items.push_back(new QStandardItem("无"));
        items.push_back(new QStandardItem(infor->_descripe));
        items[0]->setData(QVariant(int(Yzty::FileToolCode::PILLEGAL)*Yzty::ToolMapItemHashCode+_models[code][tcode]->rowCount()));
        _models[code][tcode]->appendRow(items);
    }

}

void FileWidget::UpDateFileOpRecord(Yzty::MODECODE ,Yzty::ITEMCODE ){

}

void FileWidget::UpDateFilePw(Yzty::MODECODE ,Yzty::ITEMCODE ){

}

void FileWidget::UpDateFileExtern(Yzty::MODECODE ,Yzty::ITEMCODE ){

}

void FileWidget::KeyWord_Combox_Change(int pi,int pj){
    UpdateData(pi,pj);
}

void FileWidget::MouseRightMenu(const QPoint& pos)
{
    int i=ui->stackedWidget->currentIndex();
    curindex=_views[i][_tabws[i]->currentIndex()]->indexAt(pos);  //通过坐标获取Index
    if(!curindex.isValid())
        return;
    this->_tableviewmenu->exec(cursor().pos());
}

void FileWidget::OpenFile()
{
    static int filenamecol=0,filepathcol=3;
    //每一行的默认第一列是文件名,每一行的默认第四列是文件的路径

    if(!curindex.isValid())
        return;

    int i=ui->stackedWidget->currentIndex();
    QString path=_models[i][_tabws[i]->currentIndex()]->index(curindex.row(),filepathcol).data().toString();
    QString filename=_models[i][_tabws[i]->currentIndex()]->index(curindex.row(),filenamecol).data().toString();
    QDesktopServices::openUrl(QUrl("file:"+path+"/"+filename,QUrl::TolerantMode));   //打开文件
}
void FileWidget::OpenFileLocation()
{
    static int filenamecol=0,filepathcol=3;
    //每一行的默认第一列是文件名,每一行的默认第四列是文件的路径


    if(!curindex.isValid())
        return;

    int i=ui->stackedWidget->currentIndex();
    QString path=_models[i][_tabws[i]->currentIndex()]->index(curindex.row(),filepathcol).data().toString();
    QString filename=_models[i][_tabws[i]->currentIndex()]->index(curindex.row(),filenamecol).data().toString();

    QFileDialog dialog(this, tr("Open Files"));
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
    dialog.setModal(QFileDialog::ExistingFiles);
    dialog.setOption(QFileDialog::DontUseNativeDialog);
    dialog.selectUrl(QUrl("file:"+path+"/"+filename));
    dialog.selectFile(filename);
    dialog.exec();

}
void FileWidget::LevelJudge(int i){
    int indexsource=ui->stackedWidget->currentIndex();
    int indexj=curindex.row();
    Yzty::FileToolCode indextarget;
    QVector<Yzty::BaceInfor*> infors1=FileInfor::GetInstance()->GetModifiableInfor(indexsource);

    if(indexj>infors1.size()-1)
        return;
    if(i<4){
        if(indexsource<(int)Yzty::FileToolCode::PTOTAL)
            indextarget=Yzty::FileToolCode::FILLEGAL;
        else
            indextarget=Yzty::FileToolCode::PILLEGAL;
    }
    else if(i==4){
        if(indexsource<(int)Yzty::FileToolCode::PTOTAL)
            indextarget=Yzty::FileToolCode::FOTHER;
        else
            indextarget=Yzty::FileToolCode::POTHER;
    }
    else{
        if(indexsource<(int)Yzty::FileToolCode::PTOTAL)
            indextarget=Yzty::FileToolCode::FKEY;
        else
            indextarget=Yzty::FileToolCode::PKEY;
    }

    Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor *>(infors1.takeAt(indexj));
    FileInfor::GetInstance()->removeByPerson(indexsource,infor);
    UpdateData(indexsource);

    if(i<4)
        infor->_result=IllegaLevel[0];
    else if(i==4)
        infor->_result=IllegaLevel[1];
    else
        infor->_result=IllegaLevel[2];

    infor->_pwlevel=FilePwLevel.at(i);
    FileInfor::GetInstance()->AddByperson((int)indextarget,infor);
    UpdateData((int)indextarget);
}

void FileWidget::Judge(QAction* action){
    int i=ui->stackedWidget->currentIndex();
    int j = curindex.row();
    int indexmenu = action->data().toInt(); //获取点击菜单的index
    Filejudge judgedialog;
    judgedialog.setWindowTitle("增加密级初判人");
    judgedialog.setWindowFlag(Qt::Dialog);
    int statu = judgedialog.exec();
    if(statu == 0) {            //点击了取消
        return;
    }
    Yzty::FileLevelPerson levelinfor = judgedialog.GetLevelPerFromDialog(); //获取对话框信息
//    levelinfor._pwlevel = FilePwLevel[indexmenu];
    FileInfor::GetInstance()->SetNewFileInfor(levelinfor,i,j);         //设置新数据
//    this->UpdateData(i);                //更新前端
    LevelJudge(indexmenu);
}

void FileWidget::RowSelect_Paint(int row,int i,int j){
    std::cout<<"row-i-j: "<<row<<" "<<i<<" "<<j<<std::endl;
    QModelIndex index=_models[i][j]->index(row,0);
    int mcode=index.data(Qt::UserRole+1).toInt()/Yzty::ToolMapItemHashCode;

    QVector<Yzty::BaceInfor*> infors=FileInfor::GetInstance()->GetModifiableInfor(int(Yzty::FileToolCode(mcode)));
    Yzty::FileRecordInfor* infor=dynamic_cast<Yzty::FileRecordInfor*>(infors[row]);

    auto findx=[](uint64_t code)->int{
        for(int i=0;i<63;i++)
            if((code>>i)&1) return i;
        return 0;
    };

    const QMap<Yzty::FileTypeLimit::Type,QFileInfoList>& paths=Yzty::FileFilter::GetInstance()->GetAllSelectAblePahts();

    _plains[i][j]->clear();
    _plains[i][j]->appendHtml(QString("<h4>文件类型: %1</h4>").arg(Yzty::FileTypeLimit::TypeNames[findx(infor->_filerecord->_type)]));
    _plains[i][j]->appendHtml(QString("<h4>文件路径: %1</h4>").arg(paths[infor->_filerecord->_type][int(infor->_filerecord->_file_id)].filePath()));
    _plains[i][j]->appendHtml("</hr>");

  /*  for(Yzty::FileKeyWordPos& temp:infor->_filerecord->_key_pos){
        QString key=KeyWordLib::GetInstance()->GetWord(temp._lib_id,int(temp._keyword_id));
        _plains[i][j]->appendHtml(QString("关键字: %1  位置: %2行-%3列-%4页").arg(key).arg(temp._row).arg(temp._col).arg(temp._page));

    }
*/
    QString tem_infor = infor->_filerecord->_hitSubstrings;
    for(Yzty::FileKeyWordPos& temp:infor->_filerecord->_key_pos){
        QString key=KeyWordLib::GetInstance()->GetWord(temp._lib_id,int(temp._keyword_id));
        tem_infor.replace(key,"<span style=\"background-color: yellow;\">" + key + "</span>");
    }
    _plains[i][j]->appendHtml(tem_infor);
}

void FileWidget::UpDatecnts(){
//    _updatemutex.lock();
    int n=_numbers.size();
    QVector<int> tmp(3);
    tmp[0]=FileInfor::GetInstance()->GetFilecnts();
    tmp[1]=FileInfor::GetInstance()->GetCheckcnts();
    tmp[2]=FileInfor::GetInstance()->GetHitcnts();
    for(int i = 0;i<n;i++) {
        for(int j=0;j<labels.size();j++){
            _numbers[i][j]->clear();
            _numbers[i][j]->setText(labels[j]+QString::number(tmp[j]));
            _numbers[i][j]->update();
        }
    }
//   _updatemutex.unlock();
}


void FileWidget::doMouseTrackTip(QModelIndex index)
{
    QToolTip::showText(QCursor::pos(), index.data().toString());
}
